package cn.cloud9;

public class Case3HundredChickens {

    /**
     * http://c.biancheng.net/view/515.html
     * 中国古代数学家张丘建在他的《算经》中提出了一个著名的“百钱买百鸡问题”，
     * 鸡翁一，值钱五，
     * 鸡母一，值钱三，
     * 鸡雏三，值钱一，
     * 百钱买百鸡，问翁、母、雏各几何？
     * @param args
     */
    public static void main(String[] args) {
        // 定义三种产品的数量和价格
        final int AMOUNT = 0;
        final int PRICE = 1;
        final int[] COCK = {1, 5};
        final int[] HEN = {1, 3};
        final int[] CHICK = {3, 1};
        // 定义预算数量
        final int BUDGET = 100;
        final int TOTAL_AMOUNT = 100;
        // 穷举法计算
        for (int cockAmount = 0; cockAmount <= BUDGET / COCK[PRICE]; cockAmount ++) {
            for (int henAmount = 0; henAmount <= BUDGET / HEN[PRICE]; henAmount ++) {
                for (int chickAmount = 3; chickAmount <= BUDGET / CHICK[PRICE]; chickAmount ++) {
                    // 判断是否满预算？ 这里有个问题就是小鸡是需要计算整除自己的单价个数才知道花了多少钱，
                    // 其他种类也需要，只是因为是1，所以除不除都一样
                    boolean fullBudget =
                            BUDGET ==
                                    // 索性都改成这样计算，不会出问题了
                                    (cockAmount / COCK[AMOUNT]) * COCK[PRICE] +
                                    (henAmount / HEN[AMOUNT]) * HEN[PRICE] +
                                    (chickAmount / CHICK[AMOUNT]) * CHICK[PRICE];
                    // 判断是否满数量？
                    boolean fullTotalAmount =
                            TOTAL_AMOUNT == cockAmount + henAmount + chickAmount;
                    // 判断小鸡的购买数是不是取整的
                    boolean isFullChick =
                            chickAmount % CHICK[AMOUNT] == 0;
                    // 上述条件都成立，则是符合题目要求的
                    if (fullBudget && fullTotalAmount && isFullChick) {
                        System.out.printf("公鸡数%d, 母鸡数%d, 小鸡数%d\n", cockAmount, henAmount, chickAmount);
                    }
                }
            }
        }

        solution2();
    }

    /**
     * https://www.bilibili.com/video/BV18J411W7cE?p=93
     * 中国古代数学家张丘建在他的《算经》中提出了一个著名的“百钱买百鸡问题”，
     * 鸡翁一，值钱五，
     * 鸡母一，值钱三，
     * 鸡雏三，值钱一，
     * 百钱买百鸡，问翁、母、雏各几何？
     * @param
     */
    public static void solution2() {
        System.out.println("- - - - 分隔符 - - - -");
        final int BUDGET = 100;
        final int TOTAL_AMOUNT = 100;

        final int AMOUNT = 0;
        final int PRICE = 1;
        final int[] COCK = {1, 5};
        final int[] HEN = {1, 3};
        final int[] CHICK = {3, 1};

        final int MAX_COCK_AMOUNT = TOTAL_AMOUNT / COCK[PRICE] / COCK[AMOUNT];
        final int MAX_HEN_AMOUNT = TOTAL_AMOUNT / HEN[PRICE] / HEN[AMOUNT];

        for (int cockAmountRange = 0; cockAmountRange < MAX_COCK_AMOUNT; cockAmountRange++) {
            for (int henAmountRange = 0; henAmountRange < MAX_HEN_AMOUNT; henAmountRange++) {
                int chickAmountRange = TOTAL_AMOUNT - cockAmountRange - henAmountRange;

                // 是否整除
                boolean isComplete = chickAmountRange % CHICK[AMOUNT] == 0;
                // 预算是否达到100
                boolean isFull =
                    BUDGET ==
                        (chickAmountRange / CHICK[AMOUNT] * CHICK[PRICE]) +
                        (henAmountRange / HEN[AMOUNT] * HEN[PRICE]) +
                        (cockAmountRange / COCK[AMOUNT] * COCK[PRICE])
                ;

                if (isComplete && isFull) {
                    System.out.printf("公鸡数%d, 母鸡数%d, 小鸡数%d\n", cockAmountRange, henAmountRange, chickAmountRange);
                }
            }
        }

    }
}
